using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSShell
{
    public partial class Shell : Form
    {
        public Shell()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Resize the main form to fit for the inner form of the tabpage.
        /// </summary>
        public void ResizeMainForm()
        {
            // Find button1 and get it's current Location x and y.
            int curButton1LocationX = this.button1.Location.X;
            int curButton1LocationY = this.button1.Location.Y;

            // Find tabcontrol1 and get its current Size x and y.
            int curTabControl1SizeWidth = this.tabcontrol1.Size.Width;
            int curTabControl1SizeHeight = this.tabcontrol1.Size.Height;
            
            // Find the shell(main form) and get it's current ClientSize x and y. // ClientSize or Size ?
            int curMainFormSizeWidth = this.Size.Width;
            int curMainFormSizeHeight = this.Size.Height;

            if (widthIncrement == 0 || heightIncrement == 0)
            {
                return;
            }

            this.SuspendLayout();
            this.button1.Location = new System.Drawing.Point(curButton1LocationX + widthIncrement, curButton1LocationY + heightIncrement);
            this.tabcontrol1.Size = new System.Drawing.Size(curTabControl1SizeWidth + widthIncrement, curTabControl1SizeHeight + heightIncrement);
            this.Size = new System.Drawing.Size(curMainFormSizeWidth + widthIncrement, curMainFormSizeHeight + heightIncrement);
            this.ResumeLayout();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tabcontrol1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Plugin tabControl = sender as Plugin;
            int index = tabControl.SelectedIndex;
            TabPage selectedPage = tabControl.SelectedTab;
            TabPage.ControlCollection controls = selectedPage.Controls as TabPage.ControlCollection;
            
            foreach (Control control in controls)
            {
                if (control.GetType().BaseType.Name == "Form")
                {
                    widthIncrement = control.Size.Width - widthOfFormInPreviousTabPage;
                    heightIncrement = control.Size.Height - heightOfFormInPreviousTabPage;
                    break;
                }
            }

            ResizeMainForm();
        }

        private int widthOfFormInPreviousTabPage = 0;
        private int heightOfFormInPreviousTabPage = 0;

        private int widthIncrement = 0;
        private int heightIncrement = 0;

        private void tabcontrol1_Deselected(object sender, TabControlEventArgs e)
        {
            Plugin tabControl = sender as Plugin;
            int index = tabControl.SelectedIndex;
            TabPage selectedPage = tabControl.SelectedTab;

            TabPage.ControlCollection controls = selectedPage.Controls as TabPage.ControlCollection;
            foreach (Control control in controls)
            {
                if (control.GetType().BaseType.Name == "Form")
                {
                    widthOfFormInPreviousTabPage = control.Size.Width;
                    heightOfFormInPreviousTabPage = control.Size.Height;
                    break;
                }
            }
        }

        private void Shell_Load(object sender, EventArgs e)
        {
            // Resize the main form based on the inner form size of first tab page.
            InitResizeMainForm();
        }

        private void InitResizeMainForm()
        {
            Size firstInnerFormSize = tabcontrol1.GetFirstInnerFormSize();

            const int defaultWidth = 600;
            const int defaultHeight = 400;

            widthIncrement = firstInnerFormSize.Width - defaultWidth;
            heightIncrement = firstInnerFormSize.Height - defaultHeight;
            ResizeMainForm();
        }
    }
}